﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using lucienneCRMApp.Models;
using System.Data.Objects.SqlClient;
using System.Web.Security;
using System.Web.WebPages.Html;
using System.Net.Mail;



namespace lucienneCRMApp.Controllers
{
    public class PeopleController : Controller
    {
        private lucienneCRMAppContext context = new lucienneCRMAppContext();


        //
        // GET: /People/

        /*public ViewResult Index()
        {
            return View(context.People.ToList());
        }*/

        public ActionResult Index()
        {
            var data = context.People.Include(a => a.typesmodel);
            return View(data.ToList());
        }

      
        


        //
        // GET: /People/Details/5

        public ActionResult Details(int id)
        {
            Person person = context.People.Single(x => x.PersonId == id);
            return View(person);
        }

        //
        // GET: /People/Create

        public ActionResult Create()
        {
            this.ViewData["StaffType"] = new SelectList(context.TypesModels, "TypeId", "Type");
            //this.ViewData["StaffType"] = new SelectList(context.TypesModels, "TypeId", "Type");
            //ViewBag.StaffType = new SelectList(context.TypesModels, "TypeId", "Type");
            return View();

        }

        //
        // POST: /People/Create

        [HttpPost]
        public ActionResult Create(Person pers)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    context.People.Add(pers);
                    context.SaveChanges();
                    return RedirectToAction("LogOn");
                }
                this.ViewData["StaffType"] = new SelectList(context.TypesModels, "TypeId", "Type", pers.StaffType);
                //ViewBag.StaffType = new SelectList(context.TypesModels, "TypeId", "Type", pers.StaffType);
                return View(pers);
            }
            catch
            {

                return View();
            }

        }
        //
        // GET: /People/Edit/5

        public ActionResult Edit(int id)
        {
            Person person = context.People.Single(x => x.PersonId == id);
            ViewBag.StaffType = new SelectList(context.TypesModels, "TypeId", "Type", person.StaffType);
            return View(person);
        }

        //
        // POST: /People/Edit/5

        [HttpPost]
        public ActionResult Edit(Person person)
        {
            if (ModelState.IsValid)
            {
                context.Entry(person).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StaffType = new SelectList(context.TypesModels, "TypeId", "Type", person.StaffType);
            return View(person);
        }

        //
        // GET: /People/Delete/5

        public ActionResult Delete(int id)
        {
            Person person = context.People.Single(x => x.PersonId == id);
            return View(person);
        }

        //
        // POST: /People/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Person person = context.People.Single(x => x.PersonId == id);
            context.People.Remove(person);
            context.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Account/LogOn

        public ActionResult LogOn()
        {
            return View();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            
            List<Person> perslist = new List<Person>();
            foreach(var obj in context.People.ToList())
            {
                perslist.Add(obj);
            }

            foreach (var obj in perslist)
            {
                if (obj.emailAdd == model.emailAdd && obj.password == model.password)
                {
                    if (obj.StaffType == 1)
                    {
                       
                        return RedirectToAction("Admin");
                    }
                    else if (obj.StaffType == 2) //user is a client
                    {
                        
                        return RedirectToAction("Client");
                    }

                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
             }
                        

            // If we got this far, something failed, redisplay form
            return View(model);
        }

      
       
        
        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            ViewData["Logoff"] = true;
            //return PartialView("_LogOnPartial");
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Client()
        {
            return View();
        }

        public ActionResult Admin()
        {
            return View();
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                context.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

